package rest

import (
	"reflect"
	"net/http"
)

type ErrorModel struct {
	url_model
	ErrorMessage string
}

func Error(context interface{}, msg string) interface{} {
	var controller_val reflect.Value
	if reflect.ValueOf(context).Kind() == reflect.Ptr {
		controller_val = reflect.ValueOf(context)
	} else {
		controller_val = reflect.ValueOf(&context)
	}

	if restful_controller, success := controller_val.Interface().(RestFulController); success {
		return &ErrorModel{
			url_model: url_model{
				Url: restful_controller.GetSourceName(),
			},
			ErrorMessage: msg,
		}
	}

	if request, success := controller_val.Interface().(*http.Request); success {
		return &ErrorModel{
			url_model: url_model{
				Url: request.RequestURI,
			},
			ErrorMessage: msg,
		}
	}

	return &ErrorModel{
		ErrorMessage: msg,
	}
}
